package cc.utils;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.ArrayList;

public class FileParser 
{
    public HashMap<String, ArrayList> table;
    
    public FileParser()
    {
        table = new HashMap<String, ArrayList>();
        for(long i=0; i<10 ; i++)
        {
            ArrayList<String> row = new ArrayList<String>(); 
          //  row.add("id_camp");
            row.add("nome_struc");
            row.add("Col");
            row.add("start");
            row.add("end");
            table.put(""+i, row);
        }
    }
    
    private long noOfRecords(File fileName)
    {
    	long intCount = 0;
    	try
    	{
    		FileReader frFile = new FileReader(fileName);
    		char[] ch = new char[1];
    		long i = frFile.read(ch);
    		while(i != -1)
    		{
    			if(ch[0] == '\n')
    				intCount++;
    			i = frFile.read(ch);
    		}
    		return intCount++;
    	}
    	catch(Exception e)
    	{
            e.printStackTrace();
        }
    	return 0;
    }
    
   public HashMap<String, ArrayList> parseFile(File fileName, HashMap<String, ArrayList> aryTabStruct)
    {
        try
        {
            //FileReader frFile = new FileReader(fileName);
            BufferedReader br=new BufferedReader(new FileReader(fileName));
            long intNoRecs = noOfRecords(fileName);

            HashMap<String, ArrayList> hmapResult = new HashMap<String, ArrayList>();
            long len = aryTabStruct.size();
            
            //System.out.println("\n\n\nHashLength: "+len);
            String cols="";
            String vals="";
            String sql="";
            String id_cid="";
            String nome_struc="";
            String mail="";
    
            // table structure
            // loop till no of recs are exists
             ArrayList<String> aryResultRow = null;
            
           // for(int i=0 ; i<intNoRecs ; i++)
            //{
            String line=null;
            int i=0;
            while((line = br.readLine()) != null){
            	System.out.println();
            	try{
                for(long j=0 ; j<len ; j++)
            	{
                    @SuppressWarnings("unchecked")
                    ArrayList<String> row = (ArrayList<String>)aryTabStruct.get(j+"");
                    
                    // row structure       
                  //  System.out.println(row.get(0)+" "+row.get(1)+ " "+row.get(2)+" "+row.get(3));
                  //  id_camp=""+row.get(0);
                    mail=""+row.get(0);
                    nome_struc=""+row.get(1);
                    String col=""+row.get(2);
                    String startPos =""+ row.get(3);
                    String endPos = ""+row.get(4);
                    id_cid=""+row.get(5);
                   
                    int intStart = Integer.parseInt(startPos)-1;
                    int intEnd = Integer.parseInt(endPos);
                    
                   /* char[] chDt = new char[intEnd - intStart];
                    System.out.println("Start: "+intStart+" End:"+intEnd);
                    frFile.read(chDt, intStart, (intEnd-intStart));
                    String strVal = new String(chDt);*/
                    
                    String strVal=null;
                    System.out.println("Line: "+line);
                    strVal=line.substring(intStart,intEnd);
                    strVal=strVal.replaceAll("'","");
                    
                    //System.out.println("\n\n Vals: "+strVal);
                    
                    cols = cols + "," + col;                    
            	    vals = vals + " , " + "'" + strVal + "'" ;
                    
            	}
                
                sql="INSERT INTO ctt_import (id_mail,id_cid,nome_struc " + cols + ") VALUES (" + mail +"," +id_cid+ "," + "'"+ nome_struc +"'" + vals + ")";     
                
                 
                //System.out.println("\n\n\nInsert Sql: "+sql);
                aryResultRow=new ArrayList<String>();
                aryResultRow.add(sql);
                hmapResult.put(i+"", aryResultRow);
            	 i++;
                }catch(Exception e){
                    e.printStackTrace();
                }
                cols="";
                vals=""; 
            }
            
            return hmapResult;
        }
        catch(FileNotFoundException fnfe)
        {
            System.err.println("File not found");
        }
        catch(EOFException eofe)
        {
            System.err.println("Reached to End of File");
        }
        catch(IOException ioe)
        {
            System.err.println("IO Exception");
        }
        
        // in case of error
        return null;
    }
}